<?php
namespace app\api\controller;
use think\facade\Db;

class login extends common
{
    public function index()
    {

        $data = input('post.');

        $info = Db::name('admin')->where('username',$data['username'])->find();

        if(md5($info['key'].$data['password']) !== $info['password']){
            return re_error('账号密码错误');
        }else{
            $key = md5(ThisTime().$info['key']);

            if($info['role_id'] === 1){
                $menu = Db::name('menu')->where('status',1)->order('sort desc')->select()->toArray();

            }else{
                $role_menu = Db::name('role')->where('status',1)->where('id',$info['role_id'])->field('menu_ids')->find();

                $ids = explode(',',$role_menu['menu_ids']);

                $menu = Db::name('menu')->where('id','in',$ids)->order('sort desc')->select()->toArray();

            }

            $data_ = [
                "token"=>$key,
                'uuid'=>openssl_encrypt(base64_encode($info['id']), 'DES-ECB', $key, 0),
                'role_id'=>openssl_encrypt($info['role_id'], 'DES-ECB', $key, 0),
                'menu'=>$menu,
                'username'=>$info['username'],
                'name'=>$info['name']
            ];
//            dump(base64_decode(openssl_decrypt($data_['user_id'], 'DES-ECB', $key, 0)));
            return re_success('登录成功',$data_);
        }
    }


}
